package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.dto.*;
import com.itheima.advantagesup.entity.YddAdvpos;
import com.itheima.advantagesup.mapper.YddAdvposMapper;
import com.itheima.advantagesup.service.YddAdvposService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author iTaotao
 * @date 2023/7/27 15:09
 * @desciption: 广告位实现类
 */
@Service
public class YddAdvposServiceImpl implements YddAdvposService {
    @Autowired
    private YddAdvposMapper yddAdvposMapper;

    @Override
    public PageBean seletePage(AdvposPageDTO advposPageDTO) {
        PageHelper.startPage(Integer.parseInt(advposPageDTO.getPage()), Integer.parseInt(advposPageDTO.getLimit()));
        YddAdvpos yddAdvpos = new YddAdvpos();
        BeanUtils.copyProperties(advposPageDTO,yddAdvpos);
        Page<YddAdvpos> yddAdvposPage = yddAdvposMapper.seletePage(yddAdvpos);


        PageBean<YddAdvpos> pageBean = new PageBean<>();
        pageBean.setCount((int) yddAdvposPage.getTotal());
        pageBean.setTotalPages(yddAdvposPage.getPages());
        pageBean.setPageSize(yddAdvposPage.getPageSize());
        pageBean.setCurrentPage(yddAdvposPage.getPageNum());
        pageBean.setData(yddAdvposPage.getResult());
        return pageBean;


    }

    @Override
    public List<YddAdvpos> seleteList() {
        List<YddAdvpos> yddAdvposList = yddAdvposMapper.seleteList();
        return yddAdvposList;
    }

    @Override
    public void addAdvpos(AdvposCreateDTO advposCreateDTO) {
        YddAdvpos yddAdvpos = new YddAdvpos();
        BeanUtils.copyProperties(advposCreateDTO , yddAdvpos);
        yddAdvpos.setDefaultData("1");
        yddAdvposMapper.addAdvpos(yddAdvpos);
    }

    @Override
    public void removeAdvposById(Integer id) {

        yddAdvposMapper.deleteById(id);
    }

    @Override
    public YddAdvpos seleteById(String id) {
        Integer inId = Integer.parseInt(id);
        YddAdvpos yddAdvpos = yddAdvposMapper.seleteById(inId);
        return yddAdvpos;
    }

    @Override
    public void updateAdvpos(AdvposUpdateDTO advposUpdateDTO) {
        YddAdvpos yddAdvpos = new YddAdvpos();
        BeanUtils.copyProperties(advposUpdateDTO , yddAdvpos);
        yddAdvposMapper.updateAdvpos(yddAdvpos);
    }
}
